package com.cms.server.resolver.plugins;

import java.sql.ResultSet;
import java.util.Date;

import com.efounder.builder.base.data.DataSetUtils;
import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.eai.data.JParamObject;
import com.efounder.form.EFFormDataModel;
import com.efounder.form.io.FormDataResolverAdapter;
import com.efounder.form.server.resolver.util.SYS_MDL_VAL;
import com.efounder.sql.JStatement;
import com.metadata.bizmodel.SYS_MODEL;
import com.persistence.FLOW_TASK_LIST;

public class CMSMergerApprovalPSHTFormToNextNode extends FormDataResolverAdapter {

	//初始化FormDataModel
	public void finishSaveForm(JStatement stmt, EFFormDataModel model, JParamObject PO) throws Exception {
		EFDataSet                    billDataSet = model.getBillDataSet();
		EFDataSet                    flowDataSet = EFDataSet.getInstance();
		EFDataSet                   flowDataSet1 = EFDataSet.getInstance();
		EFDataSet                   flowDataSet2 = EFDataSet.getInstance();
		EFDataSet                    itemDataSet = EFDataSet.getInstance();
		EFRowSet                      itemRowSet;
		String                            strSql = "";
		ResultSet                             rs = null;
		long                             curTime = new Date().getTime();
		String                            F_KJQJ = billDataSet.getRowSet(0).getString(PO.GetValueByParamName(SYS_MODEL._BLFL_KJQJ_COL_, SYS_MDL_VAL.BILL_KJQJ), "");
		String                            F_GUID = billDataSet.getRowSet(0).getString(PO.GetValueByParamName(SYS_MODEL._BILL_GUID_COL_, SYS_MDL_VAL.BILL_GUID), "");
		String                            F_DJBH = billDataSet.getRowSet(0).getString(PO.GetValueByParamName(SYS_MODEL._BILL_BH_COL_, SYS_MDL_VAL.BILL_BH), null);
		String                           FLOW_ID = PO.GetValueByParamName(FLOW_TASK_LIST._FLOW_ID, "");
		String                         NODE_NAME = "";
		
		//等待分管经理确认的流程任务
		strSql = " select * from FLOW_TASK_LIST where BIZ_DATE = '" + F_KJQJ + "' and BIZ_DJBH = '" + F_DJBH + "' and "
	           + " FLOW_ID = 'PSHT_EDIT_FLOW' and RESR_STATUS = 'pending' and RESR_IN_CAUSE = 'waiting' and NODE_TAG = 'N3' order by (OP_ID + 0) desc";
		rs = stmt.executeQuery(strSql);
		flowDataSet = DataSetUtils.resultSet2DataSet(rs, flowDataSet);
		
		//等待部门确认的流程任务
		strSql = " select * from FLOW_TASK_LIST where BIZ_DATE = '" + F_KJQJ + "' and BIZ_DJBH = '" + F_DJBH + "' and "
	           + " FLOW_ID = 'PSHT_EDIT_FLOW' and TASK_STATUS = 'ending' order by (OP_ID + 0) desc";
		rs = stmt.executeQuery(strSql);
		flowDataSet1 = DataSetUtils.resultSet2DataSet(rs, flowDataSet1);
	
		//已经部门确认的流程任务
		strSql = " select * from FLOW_TASK_LIST where BIZ_DATE = '" + F_KJQJ + "' and BIZ_DJBH = '" + F_DJBH + "' and "
	           + " FLOW_ID = 'PSHT_EDIT_FLOW' and RESR_STATUS = 'pending' and RESR_IN_CAUSE = 'submit' and NODE_TAG = 'N4' order by (OP_ID + 0) desc";
		rs = stmt.executeQuery(strSql);
		flowDataSet2 = DataSetUtils.resultSet2DataSet(rs, flowDataSet2);
		
		//单据分录的部门
		strSql = " select * from PSHTMX where " + PO.GetValueByParamName(SYS_MODEL._BILL_BH_COL_, SYS_MDL_VAL.BILL_BH) + " = '" + F_DJBH + "' and "
		       + PO.GetValueByParamName(SYS_MODEL._BLFL_KJQJ_COL_, SYS_MDL_VAL.BILL_KJQJ) + " = '" + F_KJQJ + "' ";
		rs = stmt.executeQuery(strSql);
		itemDataSet = DataSetUtils.resultSet2DataSet(rs, itemDataSet);
		
		//如果待审批的节点信息等于分录信息，则提交到下一个节点
		
		if(itemDataSet.getRowCount() == (flowDataSet.getRowCount() + flowDataSet1.getRowCount() + flowDataSet2.getRowCount())) {
			//获取操作时间最大的部门节点作为下一个节点的父节点
			itemRowSet = flowDataSet.getRowSet(0);
			//更新所有部门等待审批任务节点的状态
			for(int i = 0; i <flowDataSet.getRowCount(); i++) {
				strSql = "update FLOW_TASK_LIST set RESR_STATUS = 'processed', RESR_OUT_CAUSE = 'submit' where OP_ID = '" 
					   + flowDataSet.getRowSet(i).getString("OP_ID", "") + "' and FLOW_ID = '" + FLOW_ID + "'";
				stmt.execute(strSql);
			}
			//插入下一个节点信息
			//获取提交到节点名称
			strSql = "select * from FLOW_NODE_LIST where FLOW_ID ='" + FLOW_ID + "' and NODE_ID = 'N3'";
			rs = stmt.executeQuery(strSql);
			if(rs != null && rs.next()) NODE_NAME = rs.getString("NODE_NAME");
			
		
			strSql = "insert into FLOW_TASK_LIST (OP_ID,FLOW_ID,BIZ_DJBH,BIZ_DATE,BIZ_GUID,OP_USER,OP_USER_NAME,OP_TIME,"
				   + "OP_BMBH,OP_NOTE,TASK_STATUS,OP_NODE,NODE_SRC,NODE_SRC_NAME,NODE_TAG,NODE_TAG_NAME,POP_ID,RESR_STATUS," ;
			for(int j = 1; j <= 20; j++) {
				if(PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + "0" + j, null) != null) {
			    	if(j < 10) {
			    		strSql += "F_CHAR0" + j + ",";
			    	} else {
			    		strSql += "F_CHAR" + j + ",";
			    	}
				}
			    if(PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + "0" + j, null) != null) {
			    	if(j < 10) {
			    		strSql += "F_DATE0" + j + ",";
			    	} else {
			    		strSql += "F_DATE" + j + ",";
			    	}
			    }
			    if(PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + "0" + j, null) != null) {
			    	if(j < 10) {
			    		strSql += "F_NUM0" + j + ",";
			    	} else {
			    		strSql += "F_NUM" + j + ",";
			    	}
			    }
			}

			strSql += "RESR_IN_CAUSE,RESR_OUT_CAUSE) values ('"
			        + curTime + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._FLOW_ID, "") + "', '"
				    + F_DJBH + "', '" + F_KJQJ + "', '" + F_GUID + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_USER, "")
				    + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_USER_NAME, "") + "', '"
				    + curTime + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_BMBH, "") + "', '" 
				    + PO.GetValueByParamName(FLOW_TASK_LIST._OP_NOTE, "") + "', 'processing', '"
				    + PO.GetValueByParamName(FLOW_TASK_LIST._OP_NODE, "") + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_NODE, "") + "', '"
				    + PO.GetValueByParamName(FLOW_TASK_LIST._NODE_SRC_NAME, "") + "', 'N3', '" + NODE_NAME 
				    + "', '" + itemRowSet.getString("OP_ID", "") + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._RESR_STATUS, "") 
				    + "',";
			for(int j = 1; j <= 20; j++) {
				if(PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + "0" + j, null) != null) {
					if(j < 10) {
		    			strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + "0" + j, null)  + "',";
		    		} else {
		    			strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + j, null) + "',";
		    		}
			    }
			    if(PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + "0" + j, null) != null) {
			    	if(j < 10) {
			    		strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + "0" + j, null)  + "',";
		    		} else {
		    			strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + j, null) + "',";
		    		}
			    }
				if(PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + "0" + j, null) != null) {
					if(j < 10) {
		    			strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + "0" + j, null)  + "',";
		    		} else {
		    			strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + j, null) + "',";
		    		}
				}			    	
			}
			strSql += "'submit', '')";
			stmt.execute(strSql);
		}
	}
}
